#include "h_cc2640r2f_osal.h"
#include "osal_snv.h"
#include "h_cc2640r2f_uart.h"

/*
    For CC2640R2F, user flash ID is 0x80 - 0x8F
*/

/*
    After a write or read fail, the max try times defined behind
*/
#define H_CC2640R2F_OSAL_MAXTRYTIMES            0x03

signed char H_CC2640R2F_OSAL_Read(unsigned char flash_id, \
                                    unsigned char *rdata, \
                                       unsigned short length)
{
    //signed char rslt = 0;
    unsigned char trytimes = 0U;
    
    while(trytimes++ < H_CC2640R2F_OSAL_MAXTRYTIMES)
    {
        if((osal_snv_read(flash_id, length, rdata)) == 0)
        {
            return 0;
        }
    }
    
    return trytimes;
}

signed char H_CC2640R2F_OSAL_Write(unsigned char flash_id, \
                                     unsigned char *wdata, \
                                       unsigned short length)
{
    //signed char rslt = 0;
    unsigned char trytimes = 0U;
    
    while(trytimes++ < H_CC2640R2F_OSAL_MAXTRYTIMES)
    {
        if((osal_snv_write(flash_id, length, wdata)) == 0)
        {
            return 0;
        }
    }
    
    return trytimes;
}

/*
#define H_CC2640R2F_OSAL_TestID         0x80

#define H_CC2640R2F_OSAL_TestLength     0x28

void H_CC2640R2F_OSAL_Test(void)
{
    unsigned char rdata[H_CC2640R2F_OSAL_TestLength];
    unsigned char wdata[H_CC2640R2F_OSAL_TestLength];
    
    signed char rslt = 0;
    
    unsigned short length = 0;
    
    for(; length < H_CC2640R2F_OSAL_TestLength; length++)
    {
        wdata[length] = length;
    }
    
    rslt = H_CC2640R2F_OSAL_Read(H_CC2640R2F_OSAL_TestID, \
                                    rdata, H_CC2640R2F_OSAL_TestLength);
    
    H_CC2640R2F_UART_Debug("\r\nFirst read rslt is %d and data is ", rslt);
    //H_CC2640R2F_UART_TxBuff(rdata, H_CC2640R2F_OSAL_TestLength);
    for(length = 0; length < H_CC2640R2F_OSAL_TestLength; length++)
    {
        H_CC2640R2F_UART_Debug("%x ", rdata[length]);
    }
    
    
    if(0 != rdata[0])
    {
        rslt = H_CC2640R2F_OSAL_Write(H_CC2640R2F_OSAL_TestID, \
                                    wdata, H_CC2640R2F_OSAL_TestLength);
        H_CC2640R2F_UART_Debug("\r\nThe first data is not corrected, \
                                    writed again and rslt is %d", rslt);
    }
    
    
    rslt = H_CC2640R2F_OSAL_Read(H_CC2640R2F_OSAL_TestID, \
                                    rdata, H_CC2640R2F_OSAL_TestLength);
    H_CC2640R2F_UART_Debug("\r\nSecond read rslt is %d and data is ", rslt);
    //H_CC2640R2F_UART_TxBuff(rdata, H_CC2640R2F_OSAL_TestLength);
    for(length = 0; length < H_CC2640R2F_OSAL_TestLength; length++)
    {
        H_CC2640R2F_UART_Debug("%x ", rdata[length]);
    }
}
*/
